package com.example.activitysign.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.example.activitysign.pages.AjaxResult;
import com.example.activitysign.pages.PageResult;
import com.example.activitysign.pages.PageValidate;
import com.example.activitysign.service.IActivityService;
import com.example.activitysign.validate.ActivityCreateValidate;
import com.example.activitysign.validate.ActivitySearchValidate;
import com.example.activitysign.validate.ActivityUpdateValidate;
import com.example.activitysign.vo.activity.ActivityDetailVo;
import com.example.activitysign.vo.activity.ActivityListedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 活动管理
 */
@Slf4j
@RestController
@RequestMapping("api/activity")
public class ActivityController {


    @Autowired
    private IActivityService activityService;


    @GetMapping("/deleteAllKey")
    public AjaxResult<Boolean> deleteAllKey() {
        return AjaxResult.success(activityService.deleteAllKey());
    }

    @RequestMapping("/list")
    public AjaxResult<PageResult<ActivityListedVo>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                         @RequestParam(required = false) Integer status,
                                                         @RequestParam(required = false) String name) {
        PageValidate pageValidate = new PageValidate();
        pageValidate.setPageNo(pageNo);
        pageValidate.setPageSize(pageSize);

        ActivitySearchValidate searchValidate = new ActivitySearchValidate();
        searchValidate.setStatus(status);
        searchValidate.setName(name);
        PageResult<ActivityListedVo> list = activityService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    public AjaxResult<ActivityDetailVo> detail(@RequestParam Long id) {
        ActivityDetailVo detail = activityService.detail(id);
        return AjaxResult.success(detail);
    }

    @GetMapping("/del")
    public AjaxResult<Object> del(@RequestParam Long id) {
        Boolean del = activityService.del(id);
        return !del ? AjaxResult.failed("当前活动已被参与，禁止删除！") : AjaxResult.success();
    }

    @PostMapping("/add")
    public AjaxResult<Object> add(@Validated @RequestBody ActivityCreateValidate activityAddDetailVo) {
        String val = activityAddDetailVo.getVal();
        if (val == null) {
            return AjaxResult.failed("请输入活动天数");
        }
        JSONObject jsonObject = JSON.parseObject(val, JSONObject.class);
        try {
            Integer num = jsonObject.getInteger("num");
            if (num == null) {
                return AjaxResult.failed("活动天数输入错误或任务数量与天数不匹配！");
            }
            if (num != activityAddDetailVo.getTaskList().size()) {
                return AjaxResult.failed("活动天数输入错误或任务数量与天数不匹配！");
            }
        } catch (Exception e) {
            return AjaxResult.failed("活动天数输入错误或任务数量与天数不匹配！");
        }

        activityService.add(activityAddDetailVo, 1l);
        return AjaxResult.success(true);
    }

    @PostMapping("/edit")
    public AjaxResult<Object> edit(@Validated @RequestBody ActivityUpdateValidate activityDetailVo) {
        String val = activityDetailVo.getVal();
        if (val == null) {
            return AjaxResult.failed("请输入活动天数");
        }
        JSONObject jsonObject = JSON.parseObject(val, JSONObject.class);
        try {
            Integer num = jsonObject.getInteger("num");
            if (num == null) {
                return AjaxResult.failed("活动天数输入错误或任务数量与天数不匹配！");
            }
            if (num != activityDetailVo.getTaskList().size()) {
                return AjaxResult.failed("活动天数输入错误或任务数量与天数不匹配！");
            }
        } catch (Exception e) {
            return AjaxResult.failed("活动天数输入错误或任务数量与天数不匹配！");
        }
        Boolean edit = activityService.edit(activityDetailVo, 1l);
        return !edit ? AjaxResult.failed("当前活动已被参与，禁止修改！") : AjaxResult.success();
    }
}
